#__Author:Administrator
#__Date:2019/7/18
from src import models
user_obj = None

def register():
    """
    学生注册账户
    :return:
    """
    try:
        user = input("请输用户>>")
        pwd = input("请输密码>>")
        obj = models.Student()
        obj.register(user, pwd)
        if obj:
            obj.save()
            print("注册成功")
            login()
        else:
            print("用户已经存在")
    except Exception as e:
        print(e)

def login():
    global user_obj
    user = str(input("登录用户>>"))
    pwd = str(input("用户密码>>"))
    obj = models.Student.login(user, pwd)
    if obj:
        print("欢迎%s" % user)
        user_obj = obj
       # print(user_obj.nid)
    else:
        print("登录失败")

def appy():
    #选择课程
    course_obj = models.Course.get_all_list()
    for k, v in enumerate(course_obj, 1):
        print(k, v)
    choice = int(input("选择课程>>"))
    try:
        courseNid = course_obj[choice - 1]
    except IndexError:
        print("选择不存在")
    else:
        user_obj.choice_course(courseNid)
        user_obj.pay()
        user_obj.save()
        print("【%s】课程报名成功！" % v)

def show_score():
    #查看成绩
    user_obj.show_score

def show_course():
    #查看已报名课程
    obj = user_obj.show_course
    for i in obj:
        print("\033[34m 学校：%s \n 课程：%s \033[0m" % (i.schoolId.schoolName, i.courseName))
        if user_obj.classesId:
           print("\033[34m 班级:%s \033[0m" % user_obj.allot_classes)
           for i in user_obj.get_course_teacher:
               print("\033[34m 讲师:%s 任课:%s \033[0m" % (i.teacherID.teacherName, i.courseID.courseName))
        else:
            print("\033[34m 班级：null \n 讲师: null \033[0m")

def show_choice():
    list = """
    1.注册
    2.登录
    3.报名/交学费
    4.查看已报课程
    5.查看成绩
    """
    print("\033[34m %s \033[0m" % list)

def main():
    dist = {
        "1": register,
        "2": login,
        "3": appy,
        "4": show_course,
        "5": show_score
    }
    while True:
        show_choice()
        choice = str(input("请选择操作>>"))
        if choice not in dist:
            raise Exception("选择不存在，请选择%s" % show_choice())
        dist[choice]()




